package com.weipt.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author weipt
 * @description 订单实现类
 * @date 2024/1/10 23:12
 */
@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void makeOrderFanout(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("fanout-exchange-springboot", "", orderId);
    }

    //测试整个ttl queue，过期场景
    public void makeOrderFanoutTtl(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("fanout-ttl-exchange-springboot", "", orderId);
    }

    public void makeOrderFanoutDeadTtl(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("fanout-test-ttl-exchange-springboot", "", orderId);
    }

    public void makeOrderDirect(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("direct-exchange-springboot", "sms", orderId);
        rabbitTemplate.convertAndSend("direct-exchange-springboot", "email", orderId);
        rabbitTemplate.convertAndSend("direct-exchange-springboot", "msg", orderId);
    }

    public void makeOrderTopic(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("topic-exchange-springboot", "sms", orderId);
        rabbitTemplate.convertAndSend("topic-exchange-springboot", "email", orderId);
        rabbitTemplate.convertAndSend("topic-exchange-springboot", "msg", orderId);
    }

    //测试针对某条消息设置过期时间
    public void makeOrderFanoutTtlMsg(){
        String orderId = UUID.randomUUID().toString();
        //方式1
        MessageProperties properties = new MessageProperties();
        properties.setExpiration(Long.toString(5000L));// ms
        Message amqpMessage = MessageBuilder.withBody(orderId.getBytes())
                .andProperties(properties).build();
        rabbitTemplate.convertAndSend("fanout-exchange-springboot", "", amqpMessage);

        //方式2
        MessagePostProcessor messagePostProcessor = message -> {
            message.getMessageProperties().setExpiration("5000");
            return message;
        };
        rabbitTemplate.convertAndSend("fanout-exchange-springboot", "", amqpMessage,messagePostProcessor);
    }

    /**
     * 测试死信队列
     */
    public void makeOrderFanoutDeadFull(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("fanout-test-full-exchange-springboot", "", orderId);
    }


    /**
     * 增加消息确认机制
     * 消息确认机制，后续在基于rabbitmq实现分布式事务的时候，需要用到
     */
    public void makeOrderFanoutConfirm(){
        String orderId = UUID.randomUUID().toString();
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if(ack){
                System.out.println("消息确认成功!!!!");
            }else{
                System.out.println("消息确认失败!!!!");
            }
        });
        rabbitTemplate.convertAndSend("fanout-exchange-springboot", "", orderId);
    }
}
